package cn.edu.nit.sys.controller;

import cn.edu.nit.sys.service.FileService;
import cn.edu.nit.sys.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;


    @PostMapping(value = "/upload/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R uploadImage(@RequestParam("file") MultipartFile image, @RequestParam("uid") String uid,
                         @RequestParam("type") String type, @RequestParam(value = "waterText", required = false) String waterText)
            throws FileNotFoundException {
//        MultipartFile image = fileDTO.getFile();
//        Long uid = fileDTO.getUid();
//        String type = fileDTO.getType();
        return fileService.uploadFile(image, uid, type, waterText);
    }

    @GetMapping("/avatar")
    public R getSysAvatar() {
        return fileService.getSysAvatar();
    }

    @DeleteMapping("/avatar/{id}")
    public R delSysAvatar(@PathVariable("id") String id) {
        return fileService.delSysAvatar(id);
    }

    @GetMapping("/exportExcel/{type}")
    public void exportExcel(@PathVariable("type") String type, HttpServletResponse response) {
        fileService.exportExcel(type, response);
    }

    @GetMapping("/downExcel/{name}")
    public void downloadExcel(@PathVariable("name") String fileName, HttpServletResponse response) {
        fileService.downloadExcel(fileName, response);
    }

    @PostMapping("/importData")
    public R importData(@RequestParam("file") MultipartFile file, @RequestParam("uid") String uid,
                        @RequestParam("type") String type) {
        return fileService.importData(file, uid, type);
    }
}
